package com.u.auth.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.u.auth.feign.UserFeign;
import com.u.auth.model.dto.UserFeignDTO;
import com.u.auth.model.vo.UserVO;
import com.u.auth.service.AuthService;
import com.u.common.api.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: u-social
 * @description: 授权逻辑类
 * @author: Alex Wu
 * @createDate: 2025-01-22 16:18
 **/

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private UserFeign userFeign;

    @Override
    public UserVO login(String username, String password) {
        ApiResponse<UserFeignDTO> userResp = userFeign.getUserByPassword(username, password);
        if (userResp.getCode() != 200) {
            return null;
        }
        UserFeignDTO dto = userResp.getData();

        StpUtil.login(dto.getId());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();

        UserVO userVO = new UserVO();
        userVO.setId(dto.getId());
        userVO.setName(dto.getName());
        userVO.setTokenName(tokenInfo.getTokenName());
        userVO.setTokenValue(tokenInfo.getTokenValue());
        userVO.setTokenTimeout(tokenInfo.getTokenTimeout());
        userVO.setIsLogin(tokenInfo.getIsLogin());
        return userVO;
    }

}
